package com.szholly.pro.watergas_server.water.monitorstation.zhuhai;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szholly.plug.form.excel.utils.StringUtil;
import com.szholly.pro.watergas_server.common.util.JsonUtil;
import com.szholly.pro.watergas_server.common.util.RedisDbExecute;
import com.szholly.pro.watergas_server.common.util.RedisHelper;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.RemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/ZhuhaiMonitorStation/*")
public class ZhuhaiMonitorStationController extends BaseController {

	@Autowired
	RemoteService remoteService;
	
	String redisKey = "zhuhai_guangchang_station_no";
	String orderAttr = "CYG_XZ";
	
	/**
	 * 站点设置接口
	 * stationNoToOpen 要开启的站点编号
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/stationOpen")
	public void getList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		
		String stationNoToOpen = RequestUnit.getParameter("stationNoToOpen");
		
		if(stationNoToOpen == null || stationNoToOpen.length() < 2){
			return;
		}
		
		String stationSubNoToOpen = stationNoToOpen.substring(stationNoToOpen.length()-1, stationNoToOpen.length());
		
		String openedStationNo = RedisDbExecute.getString(redisKey);
		String target_mn = RedisHelper.getInstance().getMNByStationCode(openedStationNo);
		RedisDbExecute.setString(redisKey, stationNoToOpen);
		remoteService.send3011Order(target_mn, null, null, orderAttr, stationSubNoToOpen, 1);
		
		//获取开启的站点mn号
		String openedStationDeviceNum = getOpenedStationDeviceNum();
		Map<String,String> params = queryQueryParams();
		String jsonStr = getCurrentUserJsonString();
		int endIndex = jsonStr.indexOf(",");
		jsonStr = jsonStr.substring(18, endIndex-1);
		params.put("openedMN",openedStationDeviceNum);
		params.put("operator",jsonStr);
		remoteService.insertSystemOperationLog(params);
	}	
	/**
	 * 获取开启的站点编号
	 * gislin
	 * 2017年6月19日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getOpenedStationNo")
	public void getOpenedStationNo(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String openedStationNo = RedisDbExecute.getString(redisKey);
		String openedStationDeviceNum = getOpenedStationDeviceNum();
		outString("{\"openedStationNo\":\""+openedStationNo +"\",\"openedStationDeviceNum\":\""+openedStationDeviceNum+"\"}");
	}
	
	/**
	 * 获取管段切换设置
	 * gislin
	 * 2017年6月19日
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getPileSetting")
	public void getPileSetting() throws ParseException{
		String openedStationDeviceNum = getOpenedStationDeviceNum();
		
		String dsMode = RedisDbExecute.getString("stationInfo:"+openedStationDeviceNum+":sysStatus:DS_Mode");
		String lxMode = RedisDbExecute.getString("stationInfo:"+openedStationDeviceNum+":sysStatus:LX_Mode");
		String sdMode = RedisDbExecute.getString("stationInfo:"+openedStationDeviceNum+":sysStatus:SD_Mode");
		
		String lxInterval = RedisDbExecute.getString("stationInfo:"+openedStationDeviceNum+":sysStatus:LX_JG_Set");
		
		String dsStart1 = RedisDbExecute.getString("stationInfo:"+openedStationDeviceNum+":sysStatus:DS_1_Start");
		String dsStart2 = RedisDbExecute.getString("stationInfo:"+openedStationDeviceNum+":sysStatus:DS_2_Start");
		String dsStart3 = RedisDbExecute.getString("stationInfo:"+openedStationDeviceNum+":sysStatus:DS_3_Start");
		
		HashMap<String, String> pipeSetting = new HashMap<>();
		pipeSetting.put("dsMode", dsMode);
		pipeSetting.put("lxMode", lxMode);
		pipeSetting.put("sdMode", sdMode);
		pipeSetting.put("lxInterval", lxInterval);
		pipeSetting.put("dsStart1", dsStart1);
		pipeSetting.put("dsStart2", dsStart2);
		pipeSetting.put("dsStart3", dsStart3);
		
		JSONObject json = JSONObject.fromObject(pipeSetting);
		outString(json.toString());
	}
	/**
	 * 获取当前管理的切换模式
	 * @param mn
	 */
	@RequestMapping(value="/getPipeCurrentStyle")
	public void getStatonInfo(){
		String mn = RequestUnit.getParameter("MN");
		String systemCycle = "stationInfo:"+mn+":sysStatus:SYS_CYCLE_SET";
		String systemStatus = "stationInfo:"+mn+":sysStatus:SYS_STATUS";
		String pipeDSmodelKey = "stationInfo:"+mn+":sysStatus:DS_Mode";
		String pipeLXmodelKey = "stationInfo:"+mn+":sysStatus:LX_Mode";
		String pipeSDmodelKey = "stationInfo:"+mn+":sysStatus:SD_Mode";
		String pipeStart_1Key = "stationInfo:"+mn+":sysStatus:DS_1_Start";
		String pipeStart_2Key = "stationInfo:"+mn+":sysStatus:DS_2_Start";
		String pipeStart_3Key = "stationInfo:"+mn+":sysStatus:DS_3_Start";
		String pipeLX_JGKey = "stationInfo:"+mn+":sysStatus:LX_JG_Set";
		String dsModel = RedisDbExecute.getString(pipeDSmodelKey);
		String lxModel = RedisDbExecute.getString(pipeLXmodelKey);
		String sdModel = RedisDbExecute.getString(pipeSDmodelKey);
		String start1 = RedisDbExecute.getString(pipeStart_1Key);
		String start2 = RedisDbExecute.getString(pipeStart_2Key);
		String start3 = RedisDbExecute.getString(pipeStart_3Key);
		String lxJG = RedisDbExecute.getString(pipeLX_JGKey);
		String sysStatus = RedisDbExecute.getString(systemStatus);
		String cycle = RedisDbExecute.getString(systemCycle);
		HashMap<String, String> result = new HashMap<>();
		
		result.put("SYS_STATUS", sysStatus);
		if("1.0".equals(dsModel)) {
			result.put("Mode", "定时模式");
		}
		if("1.0".equals(lxModel)) {
			result.put("Mode", "轮询模式");
		}
		if("1.0".equals(sdModel)) {
			result.put("Mode", "手动模式");
		}
		result.put("start1", start1);
		result.put("start2", start2);
		result.put("start3", start3);
		result.put("lxJG", lxJG);
		result.put("CYCLE", cycle);
		String json = JsonUtil.ConvertObjectToJson(result);
		outString(json.toString());
	}
	
	/**
	 * 获取广昌当前开启的管道mn号
	 * @return
	 * @throws ParseException
	 */
	public String getOpenedStationDeviceNum() throws ParseException{
		
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		String activityTimeStr1 = RedisDbExecute.getString("stationInfo:HY003001010001:deviceInfo:DataTime_s");
		String activityTimeStr2 = RedisDbExecute.getString("stationInfo:HY003001010002:deviceInfo:DataTime_s");
		String activityTimeStr3 = RedisDbExecute.getString("stationInfo:HY003001010003:deviceInfo:DataTime_s");

		Date activityTime1 = null;
		Date activityTime2 = null;
		Date activityTime3 = null;
		
		List<Date> dates = new ArrayList<Date>();
		
		try {
			activityTime1 = sf.parse(activityTimeStr1);
			dates.add(activityTime1);
		} catch (Exception e) {
			
		}
		
		try {
			activityTime2 = sf.parse(activityTimeStr2);
			dates.add(activityTime2);
		} catch (Exception e) {
			
		}
		
		try {
			activityTime3 = sf.parse(activityTimeStr3);
			dates.add(activityTime3);
		} catch (Exception e) {
			
		}
		
		Date maxDate = Collections.max(dates);
		
		if(maxDate == null){
			return "HY003001010001";
		}
		
		if(maxDate.equals(activityTime1)){
			return "HY003001010001";
		}
		else if(maxDate.equals(activityTime2)){
			return "HY003001010002";
		}
		else if(maxDate.equals(activityTime3)){
			return "HY003001010003";
		}
		else{
			return "HY003001010001";
		}
	}
	
	private Map<String,String> queryQueryParams() {
		Map<String,String> params = new HashMap<>();
		String STATION_STATUS = RequestUnit.getParameter("status");
		String MN = RequestUnit.getParameter("MN");
		String NAME = RequestUnit.getParameter("NAME");
		if (StringUtil.IsNotNullOrEmpty(STATION_STATUS)) {
			params.put("STATION_STATUS", STATION_STATUS);
		}
		if (StringUtil.IsNotNullOrEmpty(MN)) {
			params.put("MN", MN);
		}
		if (StringUtil.IsNotNullOrEmpty(NAME)) {
			params.put("NAME", NAME);
		}
		return params;
	}
	
	@RequestMapping(value = "/insertSystemLog")
	public void insertSystemLog() {
		String MN = RequestUnit.getParameter("MN");
		String OPERATION_ITEM = RequestUnit.getParameter("OPERATION_ITEM");
		String jsonStr = getCurrentUserJsonString();
		int endIndex = jsonStr.indexOf(",");
		jsonStr = jsonStr.substring(18, endIndex-1);
		remoteService.insertGuanLuChangeLog(MN,OPERATION_ITEM,jsonStr);
	}
	
}
